package org.winexpress

import grails.converters.JSON
import org.codehaus.groovy.grails.web.json.JSONObject;
import org.codehaus.groovy.grails.web.json.JSONArray;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;


class AdminProductController {

    // initialization: GSP support operations (must be same as GSP filename)
    /* Wine Type */
    def wineTypeCreate = { 
        println 'wine cat create '
    }
    
    def wineTypeRetrieve = { 
        println 'wine cat Retrieve '
        
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/wineType").text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    flash.message = 'No wine types.<br/><br/>'

                    redirect(uri:"/")
                }
            }
        }
        else {
            JSONArray arr = new JSONArray(txt)
            ArrayList<WineType> wineTypeList = new ArrayList<WineType>()

            arr.each()
            {
                WineType wt = new WineType()
                wt.id = it.id
                wt.typeName = it.typeName
                wt.description = it.description
                wineTypeList.add(wt)
            }

            [wineTypes:wineTypeList]
        }
    }
    
    def wineTypeUpdate = { 
        println 'wine cat update '
        
        def id = params['id']
        def typeName = params['typeName']
        def description = params['description']
        
        [wineTypeId:id, wineTypeName:typeName, wineTypeDescription:description]
    }
    
    def wineTypeDelete = { 
        println 'wine cat delete '
    }
    
    
    /* Wine Varietal */
    def wineVarietalCreate = { 
        println 'wine var create '
        
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/wineType").text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    flash.message = 'No wine types.<br/><br/>'

                    redirect(uri:"/")
                }
            }
        }
        else {
            JSONArray arr = new JSONArray(txt)
            ArrayList<WineType> wineTypeList = new ArrayList<WineType>()

            arr.each()
            {
                WineType wt = new WineType()
                wt.id = it.id
                wt.typeName = it.typeName
                wt.description = it.description
                wineTypeList.add(wt)
            }

            [wineTypes:wineTypeList]
        }
    }
    
    def wineVarietalRetrieve = { 
        println 'wine var Retrieve '
        
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/wineVarietal").text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    flash.message = 'No wine varietals.<br/><br/>'

                    redirect(uri:"/")
                }
            }
        }
        else {
            JSONArray arr  = new JSONArray(txt)
            ArrayList<WineVarietal> wineVarietalList = new ArrayList<WineVarietal>()

            arr.each()
            {
                WineVarietal wv = new WineVarietal()
                wv.id = it.id
                wv.varietalName = it.varietalName
                wv.description = it.description
                wineVarietalList.add(wt)
            }

            [wineTypes:wineTypeList]
        }
    }
    
    def wineVarietalUpdate = { 
        println 'wine var update '
        
        def id = params['id']
        def varietalName = params['varietalName']
        def description = params['description']
        
        [wineVarietalId:id, wineVarietalName:typeName, wineVarietalDescription:description]
    }
    
    def wineVarietalDelete = { 
        println 'wine var delete '
    }
    
    
    /* Wine Region */
    def wineRegionCreate = { 
        println 'wine Region create '
        
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/country").text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    flash.message = 'No countries found.<br/><br/>'

                    redirect(uri:"/")
                }
            }
        }
        else {
            JSONArray arr = new JSONArray(txt)
            ArrayList<Country> countryList = new ArrayList<Country>()

            arr.each()
            {
                Country c = new Country()
                c.id = it.id
                c.countryName = it.countryName
                countryList.add(c)
            }

            [countries:countryList]
        }
    }
    
    def wineRegionRetrieve = { 
        println 'wine Region Retrieve '
        
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/wineRegion").text
        
        if (txt.getAt(0) == '{') {
            JSONObject o = new JSONObject(txt)
            if (o.has("404")) {
                if (o.get("404") == true) {
                    flash.message = 'No wine regions.<br/><br/>'

                    redirect(uri:"/")
                }
            }
        }
        else {
            JSONArray arr  = new JSONArray(txt)
            ArrayList<WineRegion> wineRegionList = new ArrayList<WineRegion>()

            arr.each()
            {
                WineRegion wr = new WineRegion()
                wr.id = it.id
                wr.regionName = it.regionName
                wr.description = it.description
                int countryId = it.countryId
                println "front end " + countryId
                
                // consume winexpress server web service
                def countryTxt = new URL("http://localhost:8080/WineXpress/country/=" + countryId).text

                if (txt.getAt(0) == '{') {
                    JSONObject o = new JSONObject(countryTxt)
                    if (o.has("404")) {
                        if (o.get("404") == true) {
                            flash.message = 'No country found.<br/><br/>'

                            redirect(uri:"/")
                        }
                    }
                }
                else {
                    JSONObject o = new JSONObject(countryTxt)
                    wr.country = o.get("countryName")
                }
                wineRegionList.add(wr)
            }

            [wineRegions:wineRegionList]
        }
    }
    
    def wineRegionUpdate = { 
        println 'wine Region update '
        
        def id = params['id']
        def regionName = params['regionName']
        def description = params['description']
        
        [wineRegionId:id, wineRegionName:regionName, wineRegionDescription:description]
    }
    
    def wineRegionDelete = { 
        println 'wine Region delete '
    }
    
    /* Wine Product */
    def wineProductCreate = { 
        println 'wine product create '
    }
    
    def wineProductRetrieve = { 
        println 'wine product Retrieve '
        
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/product").text
        
        JSONArray arr  = new JSONArray(txt)
        ArrayList<Product> wineProductList = new ArrayList<Product>()
    
        arr.each()
        {
           Product p = new Product()
           p.id = it.id
           p.name = it.name
           p.description = it.description
           wineProductList.add(p)
        }
        
        [wineProducts:wineProductList]
    }
    
    def wineProductUpdate = { 
        println 'wine product update '
        
        def id = params['id']
        def name = params['productName']
        def description = params['description']
        
        [wineProductId:id, wineProductName:name, wineProductDescription:description]
    }
    
    def wineProductDelete = { 
        println 'wine product delete '
    }
    
    
    
    // REST-related operations for accessing WineXpress Server (for clearer view only)
    // GET
    def show = {
        
    }
    
    // PUT  
    def updateWineType = {
        def id = params['id']
        def typeName = params['typeName']        
        def description = params['description']
        
        
        // create a JSONObject to send to winexpress server
        JSONObject o = new JSONObject()
        o.put("id",id)
        o.put("typeName",typeName)
        o.put("description",description)
        
        
        // to create use PUT
        HttpClient httpclient = new DefaultHttpClient()
        HttpResponse response
        // consume winexpress server web service
        HttpPut httpput = new HttpPut("http://localhost:8080/WineXpress/wineType") // WineXpress Server's URI
        StringEntity se = new StringEntity(o.toString())     
        httpput.setEntity(se);          
        response = httpclient.execute(httpput);
        
        println "updateWineType OK"
        
        flash.message = "Wine type has been successfully updated."
        
        redirect(uri:"/wineTypeRetrieve")
    }
    
    def updateWineVarietal = {
        def id = params['id']
        def varietalName = params['varietalName']        
        def description = params['description']
        
        
        // create a JSONObject to send to winexpress server
        JSONObject o = new JSONObject()
        o.put("id",id)
        o.put("varietalName",varietalName)
        o.put("description",description)
        
        
        // to create use PUT
        HttpClient httpclient = new DefaultHttpClient()
        HttpResponse response
        // consume winexpress server web service
        HttpPut httpput = new HttpPut("http://localhost:8080/WineXpress/wineVarietal") // WineXpress Server's URI
        StringEntity se = new StringEntity(o.toString())     
        httpput.setEntity(se);          
        response = httpclient.execute(httpput);
        
        println "updateWineVarietal OK"
        
        flash.message = "Wine varietal has been successfully updated."
        
        redirect(uri:"/wineVarietalRetrieve")
    }
    
    // DELETE
    def delete = {
        
    }
    
    // POST
    def postCreateWineType = {
        println 'postCreateWineType'
        // get form values
        def typeName = params['wineTypeName']        
        def description = params['wineTypeDescription']
        
        // create a JSONObject to send to winexpress server
        JSONObject o = new JSONObject()
        o.put("typeName",typeName)
        o.put("description",description)
        
        // to create use POST
        HttpClient httpclient = new DefaultHttpClient()
        HttpResponse response
        // consume winexpress server web service
        HttpPost httppost = new HttpPost("http://localhost:8080/WineXpress/wineType") // WineXpress Server's URI
        StringEntity se = new StringEntity(o.toString())          
        httppost.setEntity(se);          
        response = httpclient.execute(httppost);

        redirect(uri:"/thankyou")
    }
    
    def postCreateWineVarietal = {
        println 'postCreateWineVarietal'
        // get form values
        def varietalName = params['wineVarietalName']        
        def description = params['wineVarietalDescription']
        def wineType = params['wineType']
        
        // create a JSONObject to send to winexpress server
        JSONObject o = new JSONObject()
        o.put("varietalName",varietalName)
        o.put("description",description)
        o.put("wineType",wineType)
        
        // to create use POST
        HttpClient httpclient = new DefaultHttpClient()
        HttpResponse response
        // consume winexpress server web service
        HttpPost httppost = new HttpPost("http://localhost:8080/WineXpress/wineVarietal") // WineXpress Server's URI
        StringEntity se = new StringEntity(o.toString())          
        httppost.setEntity(se);          
        response = httpclient.execute(httppost);

        redirect(uri:"/thankyou")
    }
    
    def postCreateWineRegion = {
        println 'postCreateWineVarietal'
        // get form values
        def regionName = params['wineRegionName']        
        def description = params['wineRegionDescription']
        def country = params['country']
        
        // create a JSONObject to send to winexpress server
        JSONObject o = new JSONObject()
        o.put("regionName",regionName)
        o.put("description",description)
        o.put("country",country)
        
        // to create use POST
        HttpClient httpclient = new DefaultHttpClient()
        HttpResponse response
        // consume winexpress server web service
        HttpPost httppost = new HttpPost("http://localhost:8080/WineXpress/wineRegion") // WineXpress Server's URI
        StringEntity se = new StringEntity(o.toString())          
        httppost.setEntity(se);          
        response = httpclient.execute(httppost);

        redirect(uri:"/thankyou")
    }
   
}
